package cpu.footprint.popup;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import cpu.footprint.R;


public class KeyBoardPopupWindow extends PopupWindow implements OnClickListener {
	private String sTitle; // 标题
	private String hHint; // 提示
	private Context context; //要调用的Acti
	public String s;
	private Keyboard1PopListener listener;  //接口监听器
	private int screen_width; //屏幕宽
	private int screen_height ;//屏幕高
	View lLayout; //
	private LinearLayout layout_createGroup; // 创建群
	private LinearLayout layout_serachGroup; // 创建群
	private Keyboard1isPupListener isPupListener; // //防止点击多次

	public KeyBoardPopupWindow(Context context) {
		this.context = context;
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		screen_width = dm.widthPixels;
		screen_height = dm.heightPixels;
		init();
		showKeyboard();
	}

	public void showKeyboard() {
		this.setContentView(lLayout);
		//设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.WRAP_CONTENT);
		//设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		//设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		//设置SelectPicPopupWindow弹出窗体动画效果
	//	this.setAnimationStyle(R.style.mystyle);
		lLayout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int height = lLayout.findViewById(R.id.keyboard1_layout).getTop();
				int y=(int) event.getY();
				if(event.getAction()==MotionEvent.ACTION_UP){
					if(y<height){
						dismiss();
						isPupListener.onIsPup(true);
					}
				}
				return true;
			}
		});
	}

	public void init() {
		lLayout = ((Activity) context).getLayoutInflater().inflate(
				R.layout.grouppopupwindow, null);
		lLayout.setAlpha((float)(0.8));
		layout_createGroup = (LinearLayout) lLayout.findViewById(R.id.layout_createGroup);
		layout_serachGroup = (LinearLayout) lLayout.findViewById(R.id.layout_serachGroup);
		layout_createGroup.setOnClickListener(this);
		layout_serachGroup.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.layout_createGroup:
				if (listener != null && isPupListener != null) {
					isPupListener.onIsPup(true);
					listener.onTimeSelect("创建");
				}
				this.dismiss();
				break;
			case R.id.layout_serachGroup:
				if (listener != null && isPupListener != null) {
					isPupListener.onIsPup(true);
					listener.onTimeSelect("搜索");
				}
				this.dismiss();
				break;
		}

	}


	public interface Keyboard1isPupListener {
		public void onIsPup(Boolean isPup);
	}

	public void setOnKeyboard1IsPupListener(
			Keyboard1isPupListener keyboard1isPupListener) {
		this.isPupListener = keyboard1isPupListener;
	}

	public void setOnKeyboard1PopListener(
			Keyboard1PopListener keyboard1PopListener) {
		this.listener = keyboard1PopListener;
	}

	public interface Keyboard1PopListener {
		public void onTimeSelect(String string);
	}
}
